package com.geeksx.trainer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geeksx.trainer.entity.KnowledgeCategory;
import com.geeksx.trainer.mapper.KnowledgeCategoryMapper;
import com.geeksx.trainer.service.KnowledgeCategoryService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 知识分类 Service 实现类
 */
@Service
public class KnowledgeCategoryServiceImpl
        extends ServiceImpl<KnowledgeCategoryMapper, KnowledgeCategory>
        implements KnowledgeCategoryService {

    @Override
    public List<Long> getAllSubCategoryIds(Long categoryId) {
        List<Long> result = new ArrayList<>();
        result.add(categoryId);

        // 查询直接子分类
        List<KnowledgeCategory> directChildren = baseMapper.selectByParentId(categoryId);
        for (KnowledgeCategory category : directChildren) {
            // 递归查询子分类的子分类
            result.addAll(getAllSubCategoryIds(category.getId()));
        }

        return result;
    }

    @Override
    public boolean deleteCategory(Long id) {
        baseMapper.deleteById(id);
        baseMapper.deleteByParentId(id);
        return true;
    }
}
